package com.lilinling.yinzhi.demand.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.hutool.http.server.HttpServerRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lilinling.common.utils.JwtInfo;
import com.lilinling.common.utils.JwtUtils;
import com.lilinling.yinzhi.demand.vo.AddDemandVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lilinling.yinzhi.demand.entity.DemandEntity;
import com.lilinling.yinzhi.demand.service.DemandService;
import com.lilinling.common.utils.PageUtils;
import com.lilinling.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * 
 *
 * @author lichenglin
 * @email lilinling15@gmail.com
 * @date 2021-03-05 17:15:03
 */
@RestController
@RequestMapping("demand/demand")
public class DemandController {
    @Autowired
    private DemandService demandService;

    /**
     * 查询最新需求
     */
    @ApiOperation("查询最新需求")
    @GetMapping("/newdemand/list")
    public R getNewDemandList () {
        QueryWrapper<DemandEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("issue_time").last("limit 4");
        List<DemandEntity> demandEntities = demandService.list(wrapper);
        return R.ok().put("newdemand",demandEntities);
    }

    /**
     * 根据用户id查询需求
     */
    @ApiOperation("根据用户id查询需求")
    @GetMapping("/auth/userdemand")
    public R getUserDemand(@Valid HttpServletRequest request){
        JwtInfo jwtInfo = JwtUtils.getUserIdByJwtToken(request);
        Long userId = jwtInfo.getId();
        List<DemandEntity> demandEntityList = demandService.getUserDemand(userId);
        return R.ok().put("data",demandEntityList);
    }


    /**
     * 发布需求
     */
    @ApiOperation("发布需求")
    @PostMapping("/auth/add")
    public R add(@Valid @RequestBody AddDemandVo addDemandVo, HttpServletRequest request){
        JwtInfo jwtInfo = JwtUtils.getUserIdByJwtToken(request);
        Long userId = jwtInfo.getId();
        demandService.addDemand(userId,addDemandVo);
        return R.ok("发布成功！");
    }

    /**
     * 根据id获取需求信息
     */
    @ApiOperation("根据id获取需求信息")
    @GetMapping("/{demandId}/addDemand")
    public AddDemandVo getAddDemandVoByDemandId(@PathVariable("demandId") Long demandId){
        AddDemandVo addDemandVo = demandService.getAddDemandVoByDemandId(demandId);
        return addDemandVo;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("demand:demand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = demandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("demand:demand:info")
    public R info(@PathVariable("id") Long id){
		DemandEntity demand = demandService.getById(id);

        return R.ok().put("demand", demand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("demand:demand:save")
    public R save(@RequestBody DemandEntity demand){
		demandService.save(demand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("demand:demand:update")
    public R update(@RequestBody DemandEntity demand){
		demandService.updateById(demand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("demand:demand:delete")
    public R delete(@RequestBody Long[] ids){
		demandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
